package com.example.elmmerchants.Ringtone_Setting;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;

public class Ringtone_Try_listen extends AppCompatActivity {

    RelativeLayout new_order_ringtone, auto_get_order_ringtone, reminder_order_ringtone, user_cancel_order_ringtone, user_refund_order_ringtone, abnormal_distribution_ringtone;
    boolean is_open_vibrate;
    int how_many_volume;
    AudioManager mAudio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ringtone_try_listen);

        mAudio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

        is_open_vibrate = getIntent().getBooleanExtra("is_open_vibrate", false);
        how_many_volume = getIntent().getIntExtra("how_many_volume", 0);

        new_order_ringtone = findViewById(R.id.new_order_ringtone);
        auto_get_order_ringtone = findViewById(R.id.auto_get_order_ringtone);
        reminder_order_ringtone = findViewById(R.id.reminder_order_ringtone);
        user_cancel_order_ringtone = findViewById(R.id.user_cancel_order_ringtone);
        user_refund_order_ringtone = findViewById(R.id.user_refund_order_ringtone);
        abnormal_distribution_ringtone = findViewById(R.id.abnormal_distribution_ringtone);

        mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, how_many_volume, AudioManager.FLAG_PLAY_SOUND);

        new_order_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (is_open_vibrate) {
                    set_vibrate();
                }
                Intent intent = new Intent(Ringtone_Try_listen.this, Get_Order_Ringtone.class);
                stopService(intent);
                intent.putExtra("ringtone_name", "新订单");
                startService(intent);
            }
        });

        auto_get_order_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (is_open_vibrate) {
                    set_vibrate();
                }
                Intent intent = new Intent(Ringtone_Try_listen.this, Get_Order_Ringtone.class);
                stopService(intent);
                intent.putExtra("ringtone_name", "自动接单");
                startService(intent);
            }
        });

        reminder_order_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (is_open_vibrate) {
                    set_vibrate();
                }
                Intent intent = new Intent(Ringtone_Try_listen.this, Get_Order_Ringtone.class);
                stopService(intent);
                intent.putExtra("ringtone_name", "催单");
                startService(intent);
            }
        });

        user_cancel_order_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (is_open_vibrate) {
                    set_vibrate();
                }
                Intent intent = new Intent(Ringtone_Try_listen.this, Get_Order_Ringtone.class);
                stopService(intent);
                intent.putExtra("ringtone_name", "订单被用户取消");
                startService(intent);
            }
        });

        user_refund_order_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (is_open_vibrate) {
                    set_vibrate();
                }
                Intent intent = new Intent(Ringtone_Try_listen.this, Get_Order_Ringtone.class);
                stopService(intent);
                intent.putExtra("ringtone_name", "用户申请退款");
                startService(intent);
            }
        });

        abnormal_distribution_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (is_open_vibrate) {
                    set_vibrate();
                }
                Intent intent = new Intent(Ringtone_Try_listen.this, Get_Order_Ringtone.class);
                stopService(intent);
                intent.putExtra("ringtone_name", "配送异常");
                startService(intent);
            }
        });
    }

    void set_vibrate() {
        final Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        final VibrationEffect vibrationEffect1;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

            // this effect creates the vibration of default amplitude for 1000ms(1 sec)
            vibrationEffect1 = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);

            // it is safe to cancel other vibrations currently taking place
            vibrator.cancel();
            vibrator.vibrate(vibrationEffect1);
        }
    }
}